var express = require('express');
var router = express.Router();
var CONFIG = require('../../../../common/config');
var CODE = require('../../../../common/code');
var superagent = require('superagent');
var usersUtil = require('./util/usersUtil');
var retUtil = require('./util/retUtil');



router.get('/',function (req, res, next) {
    res.send('respond with a user resource');
});


/***
 * 用户登录
 *
 * @author 王俊
 * @date 2018-4-12
 *
 * 参数: accName   用户名
 * 参数: accPsw    md5 后的用户密码
 *
 * 接口地址 http://yapi.yijianxiu.com:7000/project/107/interface/api/2692
 */
function userLogin(req, res, next) {
    var accName = retUtil.stringFromReq(req, 'accName');
    var accPsw = retUtil.stringFromReq(req, 'accPsw');
    if (!accName || accName.length <=0 || !accPsw || accPsw.length <= 0) {
        return retUtil.ret_err_json("参数错误", res);
    }
    var opt = {
        accName : accName,
        accPsw : accPsw
    };
    usersUtil.userLogin(opt, function (err,data) {
        if(err){
            return retUtil.ret_err_json(err, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.post('/userLogin', userLogin);
router.get('/userLogin', userLogin);


/***
 * 用户登录
 *
 * @author 王俊
 * @date 2018-4-12
 *
 * 参数: userId   用户 id
 * 参数: accName  新建用户的用户名
 * 参数: accPsw   新建用户的md5后密码
 * 参数: role     要创建的用户角色
 * 参数: newUserId要加入当前组织的用户 id
 *
 * 接口地址 http://yapi.yijianxiu.com:7000/project/107/interface/api/2692
 */
function createUser(req, res, next) {
    var userId = retUtil.numberFromReq(req, 'userId');
    var accName = retUtil.stringFromReq(req, 'accName');
    var accPsw = retUtil.stringFromReq(req, 'accPsw');
    var role = retUtil.numberFromReq(req, 'role');
    var joinUserId = retUtil.numberFromReq(req, 'joinUserId');
    if(!userId || userId <=0) {
        return retUtil.ret_err_json('参数错误', res);
    }
    if(!accName || accName.length <=0 || !accPsw || accPsw.length <=0) {
        return retUtil.ret_err_json('参数错误', null);
    }
    var roles = [1,2,4,8,16,32,64,128,256,512,1024];
    if(roles.indexOf(role) < 0) {
        return retUtil.ret_err_json('暂不支持该角色用户创建', res);
    }

    var opt = {
        userId : userId,
        accName : accName,
        accPsw : accPsw,
        role : role,
        joinUserId : joinUserId
    };
    usersUtil.createUser(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });

}
router.get('/createUser', createUser);
router.post('/createUser', createUser);


/*
* 管理员删除组织内用户
*
* @author 王俊
* @date 2018-04-12
*
* 参数: userId 管理员用户 id
* 参数: delUserId 要删除的用户 id
*
* */
function delUser(req, res, next) {
    var userId = retUtil.numberFromReq(req, 'userId');
    var delUserId = retUtil.numberFromReq(req, 'delUserId');
    if(userId <=0 || delUserId <=0) {
        return retUtil.ret_err_json('参数错误', res);
    }
    var opt = {
        userId : userId,
        delUserId : delUserId
    };
    usersUtil.adminRemoveUserFromOrg(opt, function (error, data) {
        if (error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json({}, res);
    });
}
router.get('/delUser', delUser);
router.post('/delUser', delUser);


/*
* 管理员重置用户密码
*
* @author 王俊
* @date 2018-04-12
*
* 参数: userId 管理员用户 id
* 参数: delUserId 要删除的用户 id
*
* */
function resetUserPsw(req, res, next) {
    var userId = retUtil.numberFromReq(req, 'userId');
    var updUserId = retUtil.numberFromReq(req, 'updUserId');
    var updPsw = retUtil.stringFromReq(req, 'updPsw');

    var opt = {
        userId : userId,
        updUserId :  updUserId,
        updPsw : updPsw
    };
    usersUtil.adminResetUserPsw(opt, function (error, data) {
        if (error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json({}, res);
    });
}
router.get('/resetUserPsw', resetUserPsw);
router.post('/resetUserPsw', resetUserPsw);

/*
* 获取某组织下用户列表
*
* @author 王俊
* @date 2018-04-12
*
* 参数: userId        查询者 id
* 参数: roles         角色数组
* 参数: pageNum       页码
* 参数: pageSize      页容量
* 参数: withTotalPage 是否获取总页数
* */
function getOrgUserList(req, res, next) {

    var userId = retUtil.numberFromReq(req, 'userId');
    var roles = retUtil.arrayFromReq(req, 'roles');
    var userNames = retUtil.arrayFromReq(req, 'userNames');
    var pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 12;
    var withTotalPage = retUtil.numberFromReq(req, 'withTotalPage');
    if(!userId || !roles || roles.length <=0) {
        return retUtil.ret_err_json('参数错误', res);
    }
    var opt = {
        userId : userId,
        roles : roles,
        userNames : userNames,
        pageNum : pageNum,
        pageSize : pageSize,
        withTotalPage : withTotalPage
    };
    usersUtil.adminGetOrgUsersList(opt, function (error,  data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getOrgUserList', getOrgUserList);
router.post('/getOrgUserList', getOrgUserList);

//==========================================================================
module.exports = router;